package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "c端-地址铺接口")
@Slf4j
public class AddressBookController {


    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result insertAddressBook(@RequestBody AddressBook addressBook){
        log.info("新增地址");
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }


    /**
     * 查询所有的地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有的地址信息")
    public Result<List<AddressBook>> selectAddressBook(){
        log.info("查询所有的地址信息");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> addressBookList = addressBookService.selectAllAddressBook(addressBook);
        return Result.success(addressBookList);
    }


    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> selectIsDefault(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.selectIsDefault(BaseContext.getCurrentId());
        return Result.success(addressBook);
    }


    /**
     * 通过ID查询地址信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过ID查询地址信息")
    public Result<AddressBook> selectAddressBookById(@PathVariable Long id){
        log.info("通过ID {} 查询地址",id);
        AddressBook addressBook = addressBookService.selectAddressBookById(id);
        return Result.success(addressBook);
    }


    /**
     * 根据ID修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据ID修改地址")
    public Result modifyAddressBook(@RequestBody AddressBook addressBook){
        log.info("根据ID修改地址");
        addressBookService.modifyAddressBook(addressBook);
        return Result.success();
    }


    /**
     * 根据ID删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据ID删除地址")
    public Result deleteAddressBook(Long id){
        log.info("根据ID删除地址");
        addressBookService.deleteAddressBook(id);
        return Result.success();
    }


    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址");
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

}
